nums = [7, 3, 1, 34, 6, 67, 23, 12, 9, 11]

# bubble sort
def bubble(nums, key=None, reverse=False):
  '''
  冒泡排序法
  :param nums: 待排序数据列表
  :return: 列表对象，排好序的数据列表
  '''
  for j in range(len(nums) - 2):
    flag = False
    for i in range(len(nums) - 1):
      if key:
        if key(nums[i]) > key(nums[i+1]) :
          nums[i], nums[i+1] = nums[i + 1], nums[i]
          flag = True
      else:
        if nums[i] > nums[i+1] :
          nums[i], nums[i+1] = nums[i + 1], nums[i]
          flag = True
    if not flag:
      break
  if reverse:
    return nums[::-1]
  else:
    return nums
    # print(f"第{j+1}趟排序结果为{nums}")
  # print("最终排序结果：")


if __name__ == '__main__':
  print(bubble(nums))
